<?php

// function __autoload($class_name) {
//     include 'class.' . strtolower($class_name) . '.php';
// }
require_once 'class.database.php';

class DetailDao {

    public function createDetail(Detail $detail) {
        $db = Database::getInstance();
        $con = $db->getConnection(1);

        $date_created = date('Y-m-d H:i:s');

        $query = "INSERT INTO t_det_usulan(id_usulan,nip,tgl_mulai,tgl_selesai,id_cuti,id_tujuan,date_created,user_creating) ";
        $query.=" VALUES('$detail->id_usulan','$detail->nip','$detail->tgl_mulai',
        	'$detail->tgl_selesai','$detail->id_cuti','$detail->id_tujuan','$date_created','$detail->id_user')";
        //print_r($query);
        $result = $con->prepare($query);
        $result->execute();

        if ($result->rowCount() == 1) {
            return true;
        }

        return false;
    }

    public function getDetailById($id_detail) {
        $db = Database::getInstance();
        $con = $db->getConnection(1);

        $query = "SELECT * FROM t_det_usulan WHERE id_det_usulan='$id_detail'";

        $result = $con->prepare($query);
        $result->execute();

        if ($result->rowCount() == 1) {
            return $result->fetch();
        }

        return false;
    }

    public function getDetailSelesai() {
        $db = Database::getInstance();
        $con = $db->getConnection(1);

        $query = "SELECT * FROM t_det_usulan WHERE id_level=6 AND id_buat_cuti=0";

        $result = $con->prepare($query);
        $result->execute();

        if ($result->rowCount() >= 1) {
            return $result->fetchAll();
        }

        return false;
    }
    public function updateDetailBuatCuti($id) {
        $db = Database::getInstance();
        $con = $db->getConnection(1);

        $date_created = date('Y-m-d H:i:s');

        $query = "UPDATE t_det_usulan SET id_buat_cuti=1 WHERE id_det_usulan='$id'";
       
        $result = $con->prepare($query);
        $result->execute();

        if ($result->rowCount() == 1) {
            return true;
        }

        return false;
    }

    public function getDetailByIdUsulan($id_usulan) {
        $db = Database::getInstance();
        $con = $db->getConnection(1);

        $query = "SELECT * FROM t_det_usulan WHERE id_usulan='$id_usulan'";

        $result = $con->prepare($query);
        $result->execute();

        if ($result->rowCount() >= 1) {
            return $result->fetchAll();
        }

        return false;
    }

    public function cekDetail($id_usulan,$id_level){
        $db = Database::getInstance();
        $con = $db->getConnection(1);

        $query = "SELECT * FROM t_det_usulan WHERE id_usulan='$id_usulan' AND id_level='$id_level'";

        $result = $con->prepare($query);
        $result->execute();

        if ($result->rowCount() >= 1) {
            return $result->fetchAll();
        }

        return false;
    }

    public function updateDetail(Detail $detail) {
        $db = Database::getInstance();
        $con = $db->getConnection(1);

        $date_created = date('Y-m-d H:i:s');

        $query = "UPDATE t_det_usulan SET nip='$detail->nip',tgl_mulai='$detail->tgl_mulai',
        	tgl_selesai='$detail->tgl_selesai',id_cuti='$detail->id_cuti',id_tujuan='$detail->id_tujuan' 
                     WHERE id_det_usulan='$detail->id_detail'";
       
        $result = $con->prepare($query);
        $result->execute();

        if ($result->rowCount() == 1) {
            return true;
        }

        return false;
    }
    
    public function deleteDetail($id_det_usulan){
        $db = Database::getInstance();
        $con = $db->getConnection(1);

        $query = "DELETE FROM t_det_usulan WHERE id_det_usulan='$id_det_usulan'";
       
        $result = $con->prepare($query);
        $result->execute();

        if ($result->rowCount() == 1) {
            return true;
        }

        return false;
    }

    public function prosesDetail(Detail $detail){
        $db = Database::getInstance();
        $con = $db->getConnection(1);

        $query = "UPDATE t_det_usulan SET id_level='$detail->id_level',nama_dokumen='$detail->nama_dokumen' 
            WHERE id_det_usulan='$detail->id_detail'";
        $result = $con->prepare($query);
        $result->execute();

        if ($result->rowCount() == 1) {
            return true;
        }

        return false;
    }

    public function prosesDetailDokumen(Detail $detail){
        $db = Database::getInstance();
        $con = $db->getConnection(1);

        $query = "UPDATE t_det_usulan SET id_level='$detail->id_level' WHERE id_det_usulan='$detail->id_detail'";
        //print_r($query);
        $result = $con->prepare($query);
        $result->execute();

        if ($result->rowCount() == 1) {
            return true;
        }

        return false;
    }

    public function prosesDetailPegawai(Detail $detail){
        $db = Database::getInstance();
        $con = $db->getConnection(1);

        $query = "UPDATE t_det_usulan SET id_level='$detail->id_level' WHERE id_det_usulan='$detail->id_detail'";

        $result = $con->prepare($query);
        $result->execute();

        if ($result->rowCount() == 1) {
            return true;
        }

        return false;
    }

    public function prosesDetailPegawaiByUsulan($id_usulan,$id_level){
        $db = Database::getInstance();
        $con = $db->getConnection(1);

        $query = "UPDATE t_det_usulan SET id_level='$id_level' WHERE id_usulan='$id_usulan'";

        $result = $con->prepare($query);
        $result->execute();

        if ($result->rowCount() >= 1) {
            return true;
        }

        return false;
    }

}

class Detail {

    var $id_detail;
    var $id_usulan;
    var $nip;
    var $tgl_mulai;
    var $tgl_selesai;
    var $id_cuti;
    var $id_tujuan;
    var $id_user;
    var $id_tanggapan;
    var $id_level;
    var $nama_dokumen;
    var $keterangan;

}

?>